package com.hsnn.medstgmini.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.pro.base.service.AnaDruginfoManager;
import com.hsnn.medstgmini.sys.information.service.impl.SysMessageManager;
import com.hsnn.medstgmini.sys.model.SysNotice;
import com.hsnn.medstgmini.sys.service.SysNoticeManager;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/home")
public class HomeController {
	private static final Logger log = Logger.getLogger(HomeController.class);
	
	@Autowired
	private SysNoticeManager sysNoticeManager;
	
	@Autowired
	private SysMessageManager sysMessageManager; 
	
	@Autowired
	private AnaDruginfoManager anaDruginfoManager;
	
	@Autowired
	private HttpServletRequest request;
	/**
	 * @category 跳转到首页
	 * @author 韩守松
	 * @date   2015年11月16日
	 * @param  @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("toHome")
	@ResponseBody
	public Pagination toHome(Pagination page){
		log.info("跳转到首页");
		/*
		 * 获取最新公告信息内容
		 */
//		List<SysNotice> listNotice = sysNoticeManager.getHomeSysNoticeList(page);
		List<SysNotice> listNotice = null;
		List<Map<String,Object>> listMapNotice = new ArrayList<Map<String,Object>>();
		Map<String,Object> mapNotice  = new HashMap<String, Object>();
		Map<String,List<Map<String,Object>>> resultMap = new HashMap<String, List<Map<String,Object>>>();
		if(listNotice.size() > 0 ){
			for(int i= 0; i<listNotice.size();i++ ){
				mapNotice  = (Map<String, Object>) listNotice.get(i);
				listMapNotice.add(i, mapNotice);
			}
			resultMap.put("sysNotice", listMapNotice);
		}
		
		page.setSuccess(true);
		page.setRows(resultMap);
		return page;
	}
	
	
	/**
	 * @category 获取公告详情
	 * @author 韩守松
	 * @date   2015年11月16日
	 * @param  @param sysNoticeId
	 * @param  @param model
	 * @param  @return
	 */
	@RequestMapping("toHomeNoticeDetial")
	public String toHomeNoticeDetial(String sysNoticeId, Model model){
		log.info("获取公告详情ID:"+sysNoticeId);
		SysNotice sysNotice = sysNoticeManager.getSysNoticeById(sysNoticeId);
		String projId = request.getParameter("projId");
		if(null != sysNotice){
			if(null != sysNotice.getDateTime()){
				sysNotice.setDateTimeStr(DateUtil.dateTime(sysNotice.getDateTime()));
			}
			model.addAttribute("sysNotice",sysNotice);
		}
		if(StringUtils.isNotBlank(projId)){
			model.addAttribute("projId", projId);
		}
		return "/home/homeNoticeDetail";
	}
	
	/**
	 * @category 跳转到信息详情页面
	 * @author 韩守松
	 * @date   2015年11月10日
	 * @param  @param messageId
	 * @param  @return
	 */
	@RequestMapping("/toHomeMessageDetail")
	public String toHomeMessageDetail(String messageId){
		log.info("enter toMessageDetail method");
		Map<String,Object> map = sysMessageManager.getSysMessageById(messageId);
		request.setAttribute("sysMessage", map);
		if(null != map.get("url")){
			String url = (String) map.get("url");
			String fileName = (String) map.get("fileName");
			String sysInfoAttachId = (String) map.get("sysInfoAttachId");
			List<Map<String,String>> list = new ArrayList<Map<String,String>>();
			Map<String,String> listMap = new HashMap<String, String>();
			String[] urls = url.split(",");
			String[] fileNames = fileName.split(",");
			String[] sysInfoAttachIds = sysInfoAttachId.split(",");
			for(int i = 0; i< urls.length;i++){
				listMap.put("url",urls[i]);
				listMap.put("fileName",fileNames[i]);
				listMap.put("sysInfoAttachId",sysInfoAttachIds[i]);
				list.add(i, listMap);
			}
			request.setAttribute("url",list);
		}else{
			request.setAttribute("url","");
		}
		return "/home/homeMessageDetail";
	}
	
}
